package com.blast.comicsisland.fragment;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.blast.comicsisland.R;
import com.blast.comicsisland.activity.RankItemActivity;
import com.blast.comicsisland.adapters.BookstoreRankAdapter;
import com.blast.comicsisland.model.rank.rankbody.Special;
import com.blast.comicsisland.model.rank.rankhead.Info;
import com.blast.comicsisland.model.recommend.recommendhead.AdListJson;
import com.blast.comicsisland.utils.ImageLoaderUtil;
import com.blast.comicsisland.utils.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * 为BookStroeFragment的子Fragment 榜单
 * 上方为ImageView 与TextView 二者帧布局 TextView在上层
 * 下方为本地ListView 不需要网络加载 点击事件不做
 */
public class RankFragment extends Fragment implements AdapterView.OnItemClickListener {

    private ListView rankListView;
    private Info rankHeadInfo;
    private com.blast.comicsisland.model.rank.rankbody.Info rankBodyInfo;
    private BookstoreRankAdapter bookstoreRankAdapter;
    private View view;
    private ImageLoader imageLoader;
    private ListView listView;

    public RankFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImageLoaderUtil imageLoaderUtil = ImageLoaderUtil.getInstance();
        imageLoader = imageLoaderUtil.getImageLoader();


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_rank, container, false);
        listView = (ListView) view.findViewById(R.id.bookstore_rank_list_view);
        listView.setOnItemClickListener(this);
        rankListView = (ListView) view.findViewById(R.id.bookstore_rank_list_view);
        String headPath = "http://mhjk.1391.com/comic/getproad?adgroupid=19&e=" + (System.currentTimeMillis() / 1000) + "&platformtype=1&channelid=baidu";

        RankAsyncTask myAsyncTask = new RankAsyncTask();
        myAsyncTask.execute(headPath);

        String bodyPath = "http://mhjk.1391.com/comic/recommendspecial_sb?e=" + (System.currentTimeMillis() / 1000) + "&retype=1&channelid=baidu";

        RankAsyncTask myAsyncTask1 = new RankAsyncTask();
        myAsyncTask1.execute(bodyPath);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }


    //------------------------------------->
    //listView 点击事件

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int headerViewsCount = listView.getHeaderViewsCount();
        List<Special> specials = rankBodyInfo.getSpecials();
        if (position>0) {
            Special special = specials.get(position - headerViewsCount);

            if (special != null) {
                String name = special.getName();
                int specialid = special.getSpecialid();
                Intent intent = new Intent(getActivity(), RankItemActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("specialid", specialid);
                startActivity(intent);
            }
        }
    }
    //------------------------------------->

    class RankAsyncTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            String ret = null;
            String url = params[0];
            if (url != null) {
                String jsonStr = JsonUtils.doGetRequest(url);
                ret = JsonUtils.stringToRegex(jsonStr);
            }
            return ret;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s != null) {
                try {
                    JSONObject object = new JSONObject(s);
                    JSONObject infoObject = object.getJSONObject("info");
                    if (infoObject.has("adlistjson")) {
                        rankHeadInfo = new Info();
                        rankHeadInfo.jsonParse(infoObject);

                        FrameLayout frameLayout = (FrameLayout) LayoutInflater.from(getActivity()).inflate(R.layout.head_view_bookstore_rank, rankListView, false);
                        TextView textView = (TextView) frameLayout.findViewById(R.id.bookstore_rank_text_view);
                        NetworkImageView imageView = (NetworkImageView) frameLayout.findViewById(R.id.bookstore_rank_image_view);
                        List<AdListJson> adlistjson = rankHeadInfo.getAdlistjson();
                        AdListJson adListJson = adlistjson.get(0);
                        String title = adListJson.getTitle();
                        textView.setText(title);

                        String imageurl = adListJson.getImageurl();

                        imageView.setDefaultImageResId(R.drawable.loading_land);
                        imageView.setImageUrl(imageurl, imageLoader);

                        int headerViewsCount = rankListView.getHeaderViewsCount();
                        if (headerViewsCount == 0) {
                            rankListView.addHeaderView(frameLayout);
                        }

                    } else if (infoObject.has("specials")) {
                        rankBodyInfo = new com.blast.comicsisland.model.rank.rankbody.Info();
                        rankBodyInfo.jsonParse(infoObject);

                        bookstoreRankAdapter = new BookstoreRankAdapter(getActivity(), rankBodyInfo.getSpecials());

                        rankListView.setAdapter(bookstoreRankAdapter);
                        bookstoreRankAdapter.notifyDataSetChanged();

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
